<template>
  <div v-if="visible" class="dialog-overlay" @click="close">
    <div class="dialog" @click.stop>
      <div class="dialog__header">
        <h3 class="dialog__title">重置密码</h3>
        <button class="dialog__close" @click="close" aria-label="关闭">
          <svg viewBox="0 0 24 24" width="18" height="18">
            <path fill="currentColor" d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"/>
          </svg>
        </button>
      </div>
      
      <div class="dialog__content">
        <div class="notice">
          <p>重置密码功能需要您先登录系统，然后通过个人信息页面进行密码修改。</p>
        </div>

        <div class="form-group">
          <label class="form-label">新密码</label>
          <input 
            v-model="form.newPassword" 
            type="password" 
            class="form-input" 
            placeholder="请输入新密码"
            :disabled="loading"
          />
        </div>

        <div class="form-group">
          <label class="form-label">确认密码</label>
          <input 
            v-model="form.checkPassword" 
            type="password" 
            class="form-input" 
            placeholder="请再次输入新密码"
            :disabled="loading"
            @keydown.enter="handleReset"
          />
        </div>
      </div>
      
      <div class="dialog__footer">
        <button class="btn btn--ghost" @click="close" :disabled="loading">取消</button>
        <button class="btn btn--primary" @click="handleReset" :disabled="!canReset || loading">
          <span v-if="loading">重置中...</span>
          <span v-else>重置密码</span>
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import { userApi } from '../services/user';

const props = defineProps({
  visible: { type: Boolean, default: false }
});
const emit = defineEmits(['close', 'success']);

const loading = ref(false);
const form = ref({
  newPassword: '',
  checkPassword: ''
});

const canReset = computed(() => {
  return form.value.newPassword.trim() && 
         form.value.checkPassword.trim() &&
         form.value.newPassword === form.value.checkPassword;
});

function close() {
  if (loading.value) return;
  form.value = { newPassword: '', checkPassword: '' };
  emit('close');
}

async function handleReset() {
  if (!canReset.value || loading.value) return;

  loading.value = true;
  try {
    await userApi.resetPassword(form.value);
    emit('success');
  } catch (error) {
    console.error('重置密码失败:', error);
    alert(error.message || '重置密码失败，请重试');
  } finally {
    loading.value = false;
  }
}
</script>

<style scoped>
.dialog-overlay {
  position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,.5); display: grid; place-items: center; z-index: 1005;
}
.dialog {
  background: #fff; border-radius: 12px; box-shadow: 0 20px 60px rgba(0,0,0,.2);
  width: 90%; max-width: 400px; max-height: 90vh; overflow: hidden;
}

.dialog__header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 20px 24px; border-bottom: 1px solid var(--border-color);
}
.dialog__title { font-size: 18px; font-weight: 600; margin: 0; }
.dialog__close {
  width: 32px; height: 32px; border-radius: 50%; border: none; background: transparent;
  display: grid; place-items: center; cursor: pointer;
}
.dialog__close:hover { background: #f5f6f7; }

.dialog__content { padding: 24px; }
.notice {
  background: #fef3c7; border: 1px solid #f59e0b; border-radius: 6px;
  padding: 12px; margin-bottom: 20px;
}
.notice p {
  margin: 0; font-size: 14px; color: #92400e; line-height: 1.5;
}

.form-group { display: grid; gap: 8px; margin-bottom: 16px; }
.form-label { font-size: 14px; font-weight: 600; color: #374151; }
.form-input {
  padding: 10px 12px; border: 1px solid #d1d5db; border-radius: 6px; font-size: 14px;
  transition: border-color 0.2s;
}
.form-input:focus {
  outline: none; border-color: #3b82f6; box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.1);
}
.form-input:disabled { background: #f9fafb; color: #6b7280; }

.dialog__footer {
  padding: 16px 24px; border-top: 1px solid var(--border-color);
  display: flex; justify-content: flex-end; gap: 12px;
}
.btn { padding: 8px 16px; border-radius: 6px; border: 1px solid var(--border-color); cursor: pointer; }
.btn--ghost { background: #fff; color: var(--text-primary); }
.btn--primary { background: #3b82f6; color: #fff; border-color: #3b82f6; }
.btn:hover:not(:disabled) { opacity: 0.9; }
.btn:disabled { opacity: 0.6; cursor: not-allowed; }
</style>
